// 最简单的客户端程序
#define _GNU_SOURCE

#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
    int server_socket, client_socket;
    struct addrinfo hints, *servinfo, *p;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    int rv;
    if ((rv = getaddrinfo(/*"192.168.124.12"*/"127.0.0.1", "2345", &hints, &servinfo)) != 0)
    {
        printf("getaddrinfo() %s\n", gai_strerror(rv));
        exit(0);
    }

    for (p = servinfo; p; p = servinfo->ai_next)
    {
        if ((server_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
            continue;

        if (connect(server_socket, p->ai_addr, p->ai_addrlen) < 0)
        {
            close(server_socket);
            server_socket = -1;
            continue;
        }

        break;
    }

    if (p == NULL)
    {
        printf("error\n");
        exit(0);
    }

    char message[] = "你好啊";
    send(server_socket, message, strlen(message), 0);

    char buf[1024];
    recv(server_socket, buf, 1024, 0);
    printf("responce from server is: %s\n", buf);

    return 0;
}